﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.Web.UI.WebControls;

namespace OpenSource.Controls
{
      public class TemplateRow : InputFormRow, INamingContainer // TableRow, INamingContainer
    {

        private ITemplate itemTemplate;
        private int itemIndex;
        private ListItemType itemType;
        private object dataItem;

        public TemplateRow(int itemIndex, ListItemType itemType)
        {
            this.itemIndex = itemIndex;
            this.itemType = itemType;
            //InputFormCell cell = new InputFormCell(null, 1, 1);
            Cells.Add(new TemplateCell()); // SelectCell());
        }

          public virtual object DataItem
        {
            get { return dataItem; }
            set { dataItem = value; }
        }

        public virtual int ItemIndex
        {
            get { return itemIndex; }
        }

        public virtual ListItemType ItemType
        {
            get { return itemType; }
        }


        internal void SetItemType(ListItemType itemType)
        {
            this.itemType = itemType;
        }

        /// <devdoc> 
        /// Initializes a new instance of the <see cref='System.Web.UI.WebControls.TemplateField'/> class.
        /// </devdoc> 
        public TemplateRow()
        {
        }


        /// <devdoc>
        /// <devdoc> 
        /// <para> Specifies the <see cref='System.Web.UI.ITemplate' qualify='true'/> that defines how items are rendered. </para>
        /// </devdoc> 
        [Browsable(false),
         DefaultValue(null),
         Description("SR.TemplateField_ItemTemplate"),
         PersistenceMode(PersistenceMode.InnerProperty),
         TemplateContainer(typeof (IDataItemContainer), BindingDirection.TwoWay)]
        public virtual ITemplate ItemTemplate
        {
            get { return itemTemplate; }
            set
            {
                itemTemplate = value;
                OnFieldChanged();
            }
        }

        protected override void CopyProperties(InputFormRow newField)
        {
            ((TemplateRow)newField).ItemTemplate = ItemTemplate;
            base.CopyProperties(newField);
        }

        protected override InputFormRow CreateField()
        {
            return new TemplateRow();
        }


        /// <internalonly/> 
        /// <devdoc>
        /// </devdoc> 
        public override void InitializeRow(DataControlFieldCell cell, DataControlCellType cellType,
                                            DataControlRowState rowState, int rowIndex)
        {

            base.InitializeRow(cell, cellType, rowState, rowIndex);

            ITemplate contentTemplate = this.ItemTemplate;
            if (contentTemplate != null)
            {
                // The base class might have added a control or some text for some cases
                // such as header text which need to be removed before
                // the corresponding template is used.
                // Note that setting text also has the effect of clearing out any controls. 
                //cell.Text = String.Empty;
                contentTemplate.InstantiateIn(cell);
            }
        }

        /// <devdoc> 
        /// <para>Override with an empty body if the field's controls all support callback.
        ///  Otherwise, override and throw a useful error message about why the field can't support callbacks.</para> 
        /// </devdoc>
        public override void ValidateSupportsCallback()
        {

        }
    }
}
